package com.wm.dao.hibernate;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.wm.bean.Address;
import com.wm.bean.Order;
import com.wm.dao.OrderDAO;

/**
 * @author yangbo
 *
 * 2012-12-26
 */
public class OrderHibernateDAO extends HibernateDaoSupport implements OrderDAO {

	private static final String LOAD_BY_ID = "from Order where id = ?";
	private static final String LOAD_BY_USERID = "from Order where userId = ?";
	private static final String LOAD_BY_LINKMAN = "from Order where linkman = ?";
	private static final String LOAD_BY_ADDRESS = "from Order where address like ?";
	
	/**
	 * 保存
	 * @param o
	 * @return
	 */
	public Order saveOrder(Order o){
		this.getHibernateTemplate().save(o);
		return o;
	}
	
	/**
	 * 修改
	 * @param o
	 * @return
	 */
	public Order updateOrder(Order o){
		this.getHibernateTemplate().update(o);
		return o;
	}
	
	/**
	 * 删除
	 * @param o
	 * @return
	 */
	public void removeOrder(Order o){
		this.getHibernateTemplate().delete(o);
	}
	
	/**
	 * 根据ID查询
	 * @param id
	 * @return
	 */
	public Order findOrderById(long id){
		Object[] obj={new Long(id)};
		List l = this.getHibernateTemplate().find(LOAD_BY_ID,obj);
		if(l == null || l.isEmpty()){
			return null;
		}else{
			return (Order) l.get(0);
		}
	}
	
	/**
	 * 根据用户ID查询
	 * @param userId
	 * @return
	 */
	public List findOrderByUserId(long userId){
		return this.getHibernateTemplate().find(LOAD_BY_USERID,new Long(userId));
	}
	
	/**
	 * 根据联系人查询
	 * @param linkman
	 * @return
	 */
	public List findOrderByLinkman(String linkman){
		return this.getHibernateTemplate().find(LOAD_BY_LINKMAN,linkman);
	}
	
	/**
	 * 根据地址查询
	 * @param address
	 * @return
	 */
	public List findOrderByAddress(String address){
		return this.getHibernateTemplate().find(LOAD_BY_ADDRESS,"%"+address+"%");
	}
}
